package com.valdemar.cookerybook.dao;

import java.util.Hashtable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.valdemar.cookerybook.model.Ingredient;
import com.valdemar.cookerybook.model.TranslatableEntity;
import com.valdemar.cookerybook.model.Translation;

public enum TranslationDao
{
	instance;

	// public List<Ingredient> listAllIngredients() {
	// EntityManager em = EMFService.get().createEntityManager();
	// // Read the existing entries
	// Query q = em.createQuery("select m from Ingredient m");
	// List<Ingredient> todos = q.getResultList();
	// return todos;
	// }

	public void add(String entity, TranslatableEntity entityTrans, int languageID)
	{
		synchronized (this)
		{
			EntityManager em = EMFService.get().createEntityManager();
			Translation tr = new Translation(entity, entityTrans.getCode(), entityTrans.getTranslate(), languageID);
			em.persist(tr);
			em.close();
		}
	}

	// public List<Todo> getTodos(String userId) {
	// EntityManager em = EMFService.get().createEntityManager();
	// Query q = em
	// .createQuery("select t from Todo t where t.author = :userId");
	// q.setParameter("userId", userId);
	// List<Todo> todos = q.getResultList();
	// return todos;
	// }

	public String getTranslation(String entity, String code, int languageID)
	{
		EntityManager em = EMFService.get().createEntityManager();
		Translation translation = null;
		try
		{
			Query q = em
					.createQuery("select t from Translation t where t.entity = :entity and  t.code = :code and t.languageID = :languageID");
			q.setParameter("entity", entity);
			q.setParameter("code", code);
			q.setParameter("languageID", languageID);
			translation = (Translation) q.getSingleResult();

		}
		finally
		{
			em.close();
		}
		return translation.getTranslation();
	}
	
	
	public Hashtable<String, String> getTranslations(String entity, int languageId)
	{
		
		EntityManager em = EMFService.get().createEntityManager();
		Hashtable<String, String> translations = new Hashtable<String, String>();
		
		try
		{
			Query q = em.createQuery("select t from Translation t where t.entity = :entity and t.languageID = languageID");
			q.setParameter("entity", Ingredient.class.getName());
			q.setParameter("languageID", languageId);
			
			List<Translation> trs = q.getResultList();
			
			for (Translation translation : trs)
			{
				translations.put(translation.getCode(), translation.getTranslation());
			}
			
		}
		finally
		{
			em.close();
		}
		return translations;
	}


	// public boolean remove(long id) {
	// EntityManager em = EMFService.get().createEntityManager();
	// try {
	// Ingredient ingredient = em.find(Ingredient.class, id);
	// if (ingredient == null)
	// return false;
	// em.remove(ingredient);
	// } finally {
	// em.close();
	// }
	//
	// return true;
	// }

	
	public void mergeTranslationsWithEntity(List<? extends TranslatableEntity> entityList, Hashtable<String, String> translations)
	{
		for (TranslatableEntity entity : entityList)
		{
			entity.setTranslate(translations.get(entity.getCode()));
		}
	}
	
}